package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskClientPortalUsersMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskClientPortalUsers;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskRoles;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskClientPortalUsersService;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskRolesService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskClientPortalUsersServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskClientPortalUsersMapper, TaskAutotaskClientPortalUsers>
        implements TaskAutotaskClientPortalUsersService {

    @Override
    public void sync(ResultEntity content){
        log.debug("Auto task clientPortalUsers receiver content {}", content);
        JsonNode entity = content.getEntity();

        TaskAutotaskClientPortalUsers data = TaskAutotaskClientPortalUsers.builder()
                .clientPortalUsersId(super.getLongValue(entity.get("id")))
                .dateFormat(ObjectUtils.isEmpty(entity.get("dateFormat")) ? null : entity.get("dateFormat").asInt())
                .contactID(ObjectUtils.isEmpty(entity.get("dateFormat")) ? null : entity.get("dateFormat").asInt())
                .isClientPortalActive(ObjectUtils.isEmpty(entity.get("isClientPortalActive")) || entity.get("isClientPortalActive").asBoolean())
                .numberFormat(ObjectUtils.isEmpty(entity.get("numberFormat")) ? null : entity.get("numberFormat").asInt())
                .password(ObjectUtils.isEmpty(entity.get("password"))?null : entity.get("password").asText())
                .securityLevel(ObjectUtils.isEmpty(entity.get("securityLevel")) ? null : entity.get("securityLevel").asInt())
                .timeFormat(ObjectUtils.isEmpty(entity.get("timeFormat")) ? null : entity.get("timeFormat").asInt())
                .userName(ObjectUtils.isEmpty(entity.get("password"))?null : entity.get("password").asText())
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskClientPortalUsers dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskClientPortalUsers>lambdaQuery()
                .eq(TaskAutotaskClientPortalUsers::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskClientPortalUsers::getClientPortalUsersId, data.getClientPortalUsersId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
